﻿using NHibernate;
using NHibernate.Criterion;
using System.Collections.Generic;
using MSP.Data.Domain;
using MSP.Data.NHibernate;

namespace MSP.Data.Repositories
{
    public class ProductRepository : Repository<Product>
    {
        public ProductRepository(ISession session)
            : base(session)
        {
        }

        public void SaveProduct(Product product)
        {
            Add(product);
        }

        public void UpdateProduct(Product product)
        {
            Update(product);
        }

        public void DeleteProduct(int Id)
        {
            Product product = GetProductById(Id);
            if (product != null)
                Delete(product);
        }

        public Product GetProductById(int Id)
        {
            return Session.CreateCriteria<Product>()
                          .Add(Restrictions.Eq("Id", Id))
                          .UniqueResult<Product>();
        }

        public Product GetProductByBarcode(string Barcode)
        {
            return Session.CreateCriteria<Product>()
                          .Add(Restrictions.Eq("Barcode", Barcode))
                          .UniqueResult<Product>();
        }

        public IList<Product> GetAllProduct()
        {
            return (Session.CreateCriteria<Product>()
               .List<Product>() as List<Product>);
        }
    }
}
